package com.lepu.app.fun.mother_circle.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.DateUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.DensityUtil;
import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.eyzhs.app.R;
import com.lepu.app.fun.grow.GrowHotPhotoDetailActivity;
import com.lepu.app.fun.my.PubContentDetailActivity;
import com.lepu.app.fun.my.bean.BeanMessage;

import java.util.ArrayList;

public class AdapterMotherMessage extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<BeanMessage> mArrayList = null;

	public AdapterMotherMessage(Context context,
			ArrayList<BeanMessage> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.mother_message_listitem, null);

			holder = new ViewHolder();

			LinearLayout mainItemLayout = (LinearLayout) convertView
					.findViewById(R.id.mainItemLayout);
			LinearLayout clickLayout = (LinearLayout) convertView
					.findViewById(R.id.messageClickLayout);
			CircularImageView iconImageView = (CircularImageView) convertView
					.findViewById(R.id.iconImageView);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);
			TextView timeTextView = (TextView) convertView
					.findViewById(R.id.timeTextView);

			ImageView rightTypeImageView = (ImageView) convertView
					.findViewById(R.id.rightTypeImageView);
			TextView commentTipTextView = (TextView) convertView
					.findViewById(R.id.commentTipTextView);
			ImageView photoImageView = (ImageView) convertView
					.findViewById(R.id.photoImageView);
			TextView photoDescTextView = (TextView) convertView
					.findViewById(R.id.photoDescTextView);
			TextView photoDescTextView2 = (TextView) convertView
					.findViewById(R.id.photoDescTextView2);
			ImageView imgNew = (ImageView) convertView
					.findViewById(R.id.imgNew);

			holder.mainItemLayout = mainItemLayout;
			holder.clickLayout = clickLayout;
			holder.iconImageView = iconImageView;
			holder.titleTextView = titleTextView;
			holder.timeTextView = timeTextView;
			holder.imgNew = imgNew;

			holder.rightTypeImageView = rightTypeImageView;
			holder.commentTipTextView = commentTipTextView;
			holder.photoImageView = photoImageView;
			holder.photoDescTextView = photoDescTextView;
			holder.photoDescTextView2 = photoDescTextView2;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanMessage item = mArrayList.get(position);

		UniversalImageLoadTool.disPlay(item.Avatar, holder.iconImageView,R.drawable.app_ic_default_avatar_64);
		
		holder.imgNew.setVisibility(item.IsRead==1?View.INVISIBLE:View.VISIBLE);
		holder.titleTextView.setText(item.UserNickName);
		// 服务端未返回这个字段
		holder.timeTextView.setText(DateUtil.getHMTime(item.ReplyTime));

		// 回复类型
		if (item.ReplyType.equals("1")) {
			// 点赞
			holder.commentTipTextView.setVisibility(View.GONE);
			holder.rightTypeImageView
					.setImageResource(R.drawable.icon_message_like);
		} else {
			// 回复
			holder.commentTipTextView.setVisibility(View.VISIBLE);
			holder.rightTypeImageView.setImageResource(R.drawable.icon_message_comment);
			if (!TextUtils.isEmpty(item.ReplyContent)) {
				holder.commentTipTextView.setText(item.ReplyContent);
			}
		}
		
		holder.photoImageView.setVisibility(View.GONE);
		holder.photoDescTextView.setVisibility(View.GONE);
		holder.photoDescTextView2.setVisibility(View.GONE);

		// 妈妈圈
		if (item.Board == 2) {

			if (!TextUtils.isEmpty(item.TopicTitle)) {
				holder.photoDescTextView.setVisibility(View.VISIBLE);
				holder.photoDescTextView.setText(item.TopicTitle);
			}

			// 回复的回复
			if (item.ReplyType.equals("3")) {
				holder.photoDescTextView.setVisibility(View.VISIBLE);
				holder.photoDescTextView2.setVisibility(View.VISIBLE);
				
				holder.photoDescTextView.setText(item.QuoteReplyUserNickName);
				holder.photoDescTextView2.setText(item.QuoteReplyContent);
			}
			
			holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					BeanMessage motherMessage = mArrayList.get(position);
					Intent intent = new Intent(mContext,PubContentDetailActivity.class);
					intent.putExtra("tag", 1);
					intent.putExtra("TopicId", motherMessage.TopicID);
					((BaseFragmentActivity) mContext).startActivity(intent, true);
				}
			});

		} else {
			// 热门相册
			holder.photoImageView.setVisibility(View.VISIBLE);
			holder.photoDescTextView.setVisibility(View.GONE);
			holder.photoDescTextView2.setVisibility(View.GONE);

			int widthHeight = DensityUtil.dip2px(mContext, 90);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					widthHeight * 16 / 9, widthHeight);
			holder.photoImageView.setLayoutParams(layoutParams);

			holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {

							Intent intent = new Intent(mContext,
									GrowHotPhotoDetailActivity.class);
							intent.putExtra("HotPhotoId", item.AlbumID);
							((BaseFragmentActivity) mContext).startActivity(
									intent, true);
						}
					});

			final String url = item.AlbumCover;

			if (!TextUtils.isEmpty(url)) {
				String imageUrl = Setting.getImageUrl(url);
				UniversalImageLoadTool.disPlay(imageUrl, holder.photoImageView,R.drawable.default_icon_small);
			} else {
				UniversalImageLoadTool.disPlay(ImageUtilBase.getDrawableImageUrl(R.drawable.album_cover_default),holder.photoImageView, R.drawable.album_cover_default);
			}
		}

		return convertView;
	}

	@SuppressWarnings("unused")
	private static class ViewHolder {

		LinearLayout mainItemLayout;
		LinearLayout clickLayout;
		CircularImageView iconImageView;
		TextView titleTextView;
		TextView timeTextView;
		ImageView rightTypeImageView;
		ImageView imgNew;

		TextView commentTipTextView;
		ImageView photoImageView;
		TextView photoDescTextView;
		TextView photoDescTextView2;
	}
}
